package com.xhj.bigfactory.bigFactory.volatile0;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

import static java.lang.System.out;
import static java.lang.Thread.currentThread;
import static java.lang.Thread.sleep;
/**
 * -
 *
 * @author 周沫 @Date 2021-07-25 @Time 2:23
 */
public class ABADemo {
  static AtomicReference<Integer> atomicReference = new AtomicReference<>(100);
  static AtomicStampedReference<Integer> stamped = new AtomicStampedReference<>(100, 1);

  public static void main(String[] args) {
    new Thread(
            () -> {
              try {
                sleep(600);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              out.println(currentThread().getName() + " come in...");
              stamped.compareAndSet(100, 101, stamped.getStamp(), 101);
              stamped.compareAndSet(101, 100, stamped.getStamp(), 101);
              out.println("stamped=" + stamped.getReference().intValue());
            },
            "CCC")
        .start();
    new Thread(
            () -> {
              try {
                sleep(700);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              out.println(currentThread().getName() + " come in...");
              stamped.compareAndSet(100, 1212, 1, 1212);
              out.println("stamped=" + stamped.getReference().intValue());
            },
            "DDD")
        .start();
    new Thread(
            () -> {
              try {
                sleep(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              out.println(currentThread().getName() + " come in...");
              atomicReference.compareAndSet(100, 101);
              atomicReference.compareAndSet(101, 100);
              out.println("stamped=" + atomicReference.get().intValue());
            },
            "FFF")
        .start();
    new Thread(
            () -> {
              try {
                sleep(1100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              out.println(currentThread().getName() + " come in...");
              atomicReference.compareAndSet(100, 1234);
              out.println("stamped=" + atomicReference.get().intValue());
            },
            "GGG")
        .start();

    new Thread(
            () -> {
              out.println(currentThread().getName() + " come in...");
              atomicReference.compareAndSet(100, 101);
              //              atomicReference.compareAndSet(101, 100);
              out.println("atomicReference = " + atomicReference);
            },
            "AAA")
        .start();
    new Thread(
            () -> {
              try {
                sleep(100);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              out.println(currentThread().getName() + " come in...");
              atomicReference.compareAndSet(100, 2020);
              out.println("atomicReference = " + atomicReference);
            },
            "BBB")
        .start();
  }
}

class MyDataABA {
  int number = 0; // - - - - - 多线程不可见

  public void addPlusPlus() {
    number++;
  }
}
